package com.swing.gui.pages.lock;

import base.GeneralBase;
import base.JPanelBase;
import com.swing.assets.Iurl;
import com.swing.gui.pages.Lock;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author Yuang, Gongming
 * @date 2022-01-25 14:15
 */
public class One extends JPanel implements JPanelBase {

    private static One one;

    public static One init() {
        if (one == null) {
            synchronized (One.class) {
                if (one == null) {
                    one = new One();
                    try {
                        one.css();
                        one.planning();
                        one.event();
                    } catch (Exception e) {
                        throw new RuntimeException(One.class.getSimpleName() + "实例化失败！");
                    }

                }
            }
        }
        return one;
    }

    @Override
    public void css() {
        one.setLayout(new CardLayout(100,100));
        one.setBackground(Color.LIGHT_GRAY);
    }

    @Override
    public void planning() {
        one.add(Component.init());
    }

    @Override
    public void event() {
        Component.init().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CardLayout layout = (CardLayout) Lock.init().getLayout();
                layout.show(Lock.init(), "two");
            }
        });
    }

    private static class Component extends JButton implements GeneralBase {
        private static Component component;

        public static Component init() {
            if (component == null) {
                synchronized (Component.class) {
                    if (component == null) {
                        component = new Component();
                        try {
                            component.build();
                            component.event();
                        } catch (Exception e) {
                            throw new RuntimeException(Component.class.getSimpleName() + "实例化失败！");
                        }

                    }
                }
            }
            return component;
        }

        @Override
        public void build() {
            component.setText("解锁");
            component.setFocusPainted(false);
            component.setBorderPainted(false);
//            component.setContentAreaFilled(false);
            component.setHorizontalTextPosition(SwingConstants.CENTER);
            component.setVerticalTextPosition(SwingConstants.BOTTOM);
            ImageIcon icon;
            try {
                icon = new ImageIcon(new URL(Iurl.LOCK_OFF));
                icon.setImage(icon.getImage().getScaledInstance(120, 120, Image.SCALE_AREA_AVERAGING));
                component.setIcon(icon);

                icon = new ImageIcon(new URL(Iurl.LOCK_ON));
                icon.setImage(icon.getImage().getScaledInstance(120, 120, Image.SCALE_AREA_AVERAGING));
                component.setPressedIcon(icon);
            } catch (MalformedURLException e) {
                //  ignore……
            }
            component.setLayout(new BorderLayout());
            JLabel jb = new JLabel("按住Ctrl+L解锁");
            jb.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
            component.add(jb, BorderLayout.SOUTH);
        }

        @Override
        public void event() {
            component.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(component, "解锁页面");
                }
            });
        }
    }

}
